package com.pearl.jacksoncore.demo.databind.objectmapper;



import com.fasterxml.jackson.core.JsonFactory;

import java.io.IOException;

/**
 *
 * @author TD
 * @version 1.0
 * @date 2024/3/19
 */
public class ObjectMapperFeatureTest {

    public static void main(String[] args) throws IOException {
        // MapperFeature
        // 1. 是否使用注解 （）
        // JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.USE_ANNOTATIONS, true).build();

        // 2. 针对Map和Collection类型的属性可以用getter防获取属性的引用来改变属性的值，不需要setter方法。
        //
        //对于存在getter方法，而没有对应属性值的类进行序列化时，会将getter方法对应的属性值一起序列化，转成json。
        //
        //对于存在getter方法的并且getter方法的返回值是Map或者Collection类型的类，进行序列化后，反序列化时，如果开启USE_GETTERS_AS_SETTERS，则会正常运行；如果关闭USE_GETTERS_AS_SETTERS则会抛出异常。
        //
        //原文链接：https://blog.csdn.net/zdj18166896501/article/details/125352656反序列化：在目标类中，用我们期望的值，修改那些可以被修改的属性值。
        // 正常的属性都是通过setter进行属性赋值，而Collection和Map可以直接用getter获取到属性的引用以此直接修改属性值，
        // 所以当USE_GETTERS_AS_SETTERS开启时，ObjectMapper认为Map和Collection这两种值，也应当被反序列化。

        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.USE_GETTERS_AS_SETTERS, true).build();

        // 3
        //PROPAGATE TRANSIENT MARKER(false): 如何处理被 transient 修饰的关键字。true: 会识别它(不再参与序列化/反序列
        //化)，false: 忽略这个关键字
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true).build();
        // AUTO DETECT CREATORS(rue): 是否开启自动检测创建方法的特性，true: 会找到public的构造方法 r 只有一个参数的value0f
        //方法;false:必须使用 @JsonCreator 注解去指定一个方法用于创建(空的构造方法除外，即使该特征关闭目是private都成)
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.AUTO_DETECT_CREATORS, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.AUTO_DETECT_FIELDS, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.AUTO_DETECT_GETTERS, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.AUTO_DETECT_IS_GETTERS, true).build();
//
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.AUTO_DETECT_SETTERS, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.INFER_PROPERTY_MUTATORS, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.INFER_CREATOR_FROM_CONSTRUCTOR_PROPERTIES, true).build();
//
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.ALLOW_VOID_VALUED_PROPERTIES, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.USE_STATIC_TYPING, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.USE_BASE_TYPE_AS_DEFAULT_IMPL, true).build();
//
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.INFER_BUILDER_TYPE_BINDINGS, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.REQUIRE_TYPE_ID_FOR_SUBTYPES, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.SORT_CREATOR_PROPERTIES_FIRST, true).build();
//
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.USE_STD_BEAN_NAMING, true).build();
//
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.ALLOW_EXPLICIT_PROPERTY_RENAMING, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.ALLOW_IS_GETTERS_FOR_NON_BOOLEAN, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.ALLOW_COERCION_OF_SCALARS, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.IGNORE_MERGE_FOR_UNMERGEABLE, true).build();
//
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.BLOCK_UNSAFE_POLYMORPHIC_BASE_TYPES, true).build();
        //JsonMapper jsonMapper = JsonMapper.builder().configure(MapperFeature.APPLY_DEFAULT_VALUES, true).build();
//








/*
        String userJsonStr = "{\"id\":1767798780627279873,\"name\":\"坤坤\",\"age\":18,\"org\":null,\"roleList\":[{\"id\":null,\"roleName\":\"管理员\",\"roleCode\":\"ROLE_ADMIN\"},{\"id\":null,\"roleName\":\"普通用户\",\"roleCode\":\"ROLE_USER\"}]}";
        User user = jsonMapper.readValue(userJsonStr, User.class);
        System.out.println(user);

        String json = jsonMapper.writeValueAsString(user);
        System.out.println(json);*/


        // 方式一：enable & disable 开启或禁用特性
/*        objectMapper.enable(JsonParser.Feature.AUTO_CLOSE_SOURCE); // 启用
        objectMapper.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE); // 禁用
        boolean enabled = objectMapper.isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE); // 是否开启
        System.out.println("是否开启："+ enabled);

        // 方式二：configure
        objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS,true); // 已过时


        objectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT,true);
        objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE,true);

        objectMapper.configure(SerializationFeature.CLOSE_CLOSEABLE,true);
        objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT,true);

        objectMapper.configure(JsonNodeFeature.FAIL_ON_NAN_TO_BIG_DECIMAL_COERCION,true);
        objectMapper.configure(EnumFeature.READ_ENUM_KEYS_USING_INDEX,true);*/

        JsonFactory jsonFactoryByBuilder =JsonFactory.builder()
                .enable(JsonFactory.Feature.USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING)
                .enable(JsonFactory.Feature.INTERN_FIELD_NAMES)
                .enable(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES)
                .enable(JsonFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW)
                .enable(JsonFactory.Feature.CHARSET_DETECTION)
                .build(); // 方式一：Builder 构建
    }
}
